package com.baizhi.utils;

import com.baizhi.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by 野马 on 2019/6/15.
 */
@WebFilter(urlPatterns = "/main/main.jsp" ,filterName = "WebFilterUtils")
public class WebFilterUtils implements Filter{

    @Autowired
    private HttpServletRequest request;


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("拦截器初始化");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入拦截器");
        HttpSession session = request.getSession();
        Admin admin = (Admin) session.getAttribute("admin");
        System.out.println(admin+"11111");
        if (admin != null){
            filterChain.doFilter(servletRequest, servletResponse);
        }else{
            System.out.println("else1111");
            HttpServletResponse response = (HttpServletResponse) servletResponse;
            response.sendRedirect("/cmfz/login.jsp");
        }

    }

    @Override
    public void destroy() {
        System.out.println("拦截器销毁");
    }
}
